#include "fig_conversation.h"
#include "fig_enemy_boss.h"
#include "fig_player_craft.h"
#include "gen_game_state.h"
#include "fig_vars.h"
#include "fighter.h"

const float		FIGConversation::SPEECH_DURATION = 3.0f;

FIGConversation::FIGConversation() : m_Delay(0)
{
	GENGameState* pGame = GENGameState::GetGame();
	
	m_pUITalkBoxPlayer  =  pGame->GetWidget("talk_box_p");
	m_pUITalkTextPlayer  =  pGame->GetWidget("talk_txt_p");
	m_pUIPlayerName = pGame->GetWidget("talk_name");

	m_pUITalkBoxBoss  =  pGame->GetWidget("talk_box_b");
	m_pUITalkTextBoss  =  pGame->GetWidget("talk_txt_b");

	if(m_pUITalkBoxPlayer)
		m_pUITalkBoxPlayer->SetIsVisible(false);

	if(m_pUITalkBoxBoss)
		m_pUITalkBoxBoss->SetIsVisible(false);
	
}


FIGConversation&	FIGConversation::Instance()
{
	static FIGConversation	Inst;
	return Inst;
}


void				FIGConversation::PushTalk(
												SPEECH_TYPES Type,
												const std::string& Speech
												
												)
{
	FIGConversation::CONVO_SPEECH* CS = new FIGConversation::CONVO_SPEECH();
	CS->m_Counter = SPEECH_DURATION;
	CS->m_Delay = 0.1f;
	CS->m_String = Speech;
	CS->m_Type = Type;             
	m_Conversations.push_back(CS);

}


void		FIGConversation::StartConvoLevel( float& ConvoDuration)
{
	int LevelNumber = ((FIGVars*)FIGVars::Instance())->GetCurrentLevel();
	Clear();
	m_Delay = 2.0f;
	switch(LevelNumber)
	{
		case 1:
			Conversatoin_1();
		break;

		case 2:
			Conversatoin_2();
		break;

		case 3:
			Conversatoin_3();
		break;

		case 4:
			Conversatoin_4();
		break;

		case 5:
			Conversatoin_5();
		break;

		case 6:
			Conversatoin_6();
		break;

		case 7:
			Conversatoin_7();
		break;

		default:
		break;
	}

	ConvoDuration =	 (m_Conversations.size() + 1) * SPEECH_DURATION;	
	
}

void		FIGConversation::StartConvoBossFight( float& ConvoDuration)
{
	int LevelNumber = ((FIGVars*)FIGVars::Instance())->GetCurrentLevel();
	Clear();
	switch(LevelNumber)
	{
		case 1:
			Convo_BossFight_1();
		break;

		case 2:
			Convo_BossFight_2();
		break;

		case 3:
			Convo_BossFight_3();
		break;

		case 4:
			Convo_BossFight_4();
		break;

		case 5:
			Convo_BossFight_5();
		break;

		case 6:
			Convo_BossFight_6();
		break;

		case 7:
			Convo_BossFight_7();
		break;

		default:
		break;
	}	

	ConvoDuration =	 m_Conversations.size() * SPEECH_DURATION;

}


		
void			FIGConversation::Tick(float dt)
{
	if(m_Delay > 0)
	{
		m_Delay -= dt;
		return;
	}

	ui::UIWidget* pUITalkBox = NULL;
	ui::UIWidget* pUITalkText = NULL;

	math::Vec2 Pos;
	if(m_Conversations.size() > 0)
	{
		FIGConversation::CONVO_SPEECH* CS = m_Conversations[0];

		if(CS->m_Type == SPEECH_PLAYER_1 || 
			CS->m_Type == SPEECH_PLAYER_2

			)
		{
			pUITalkBox	= m_pUITalkBoxPlayer;
			pUITalkText = m_pUITalkTextPlayer;
			Pos = m_PlayerPos;
			
		}
		else
		{
			pUITalkBox	= m_pUITalkBoxBoss;
			pUITalkText = m_pUITalkTextBoss;

			Pos = m_BossPos;
			
		}

		Pos += Fighter::Instance().GetOffset();

		if(CS->m_Delay > 0)
		{
			CS->m_Delay -= dt;
			if(CS->m_Delay <= 0)
			{
				if(pUITalkBox)
				{
				
					pUITalkBox->SetPosition(	
												Pos - 
												pUITalkBox->GetSize() * 
												0.5f );
					if(pUITalkText)
						pUITalkText->SetTextA(CS->m_String);

					if(m_pUIPlayerName)
					{

						m_pUIPlayerName->SetIsVisible(false);

						//if(CS->m_Type == SPEECH_PLAYER_2)
						//	m_pUIPlayerName->SetTextA("Mike");
						//else
						//	m_pUIPlayerName->SetTextA("Neo");

					}

					pUITalkBox->SetIsVisible(true);
				}
			}
		}
		else
		{
			CS->m_Counter -= dt;

			if(pUITalkBox)
				pUITalkBox->SetPosition(	
												Pos - 
												m_pUITalkBoxPlayer->GetSize() * 
												0.5f );

			if(CS->m_Counter <= 0)
			{
				// remove it			

				if(pUITalkBox)
					pUITalkBox->SetIsVisible(false);

				UTIL_SAFE_DELETE(CS);
				m_Conversations.erase(m_Conversations.begin());
			}
		}
	}
	

	

}

void			FIGConversation::Clear()
{
	for(int i = 0; i < (int)m_Conversations.size(); i++)
		UTIL_SAFE_DELETE(m_Conversations[i]);
	
	m_Conversations.resize(0);

	if(m_pUITalkBoxPlayer)
		m_pUITalkBoxPlayer->SetIsVisible(false);

	if(m_pUITalkBoxBoss)
		m_pUITalkBoxBoss->SetIsVisible(false);

	m_Delay = 0;
}

		